package com.fzshang.shopping.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.shopping.commen.bean.Classification;
import com.fzshang.shopping.commen.bean.CommodityClassification;
import com.fzshang.shopping.commen.bean.Good;
import com.fzshang.shopping.commen.bean.SizeClassification;
import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.entity.ApiResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @AUTHOR 冯政尚
 * @DATE 2020/3/2 20:31
 * @DAY_NAME_FULL: 星期一
 */
@Controller
@RequestMapping(value = "admin/public")
public class PublicController extends BaseController {

    /**
     * @param id
     * @return 根据商品分类id查询分类名称
     */
    @RequestMapping(value = "selectClassifitcation")
    @ResponseBody
    public ApiResult selectClassifitcation(Long id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("classification_id",id);
        return ApiResult.ok(classificationService.list(queryWrapper));
    }



    /**
     * @param id
     * @return 根据id查找信息
     */
    @RequestMapping(value = "selectGood")
    @ResponseBody
    public ApiResult selectGood(Long id){
        return ApiResult.ok(goodService.getById(id));
    }


    /**
     * @param id
     * @return 根据商家查询商品
     */
    @RequestMapping(value = "selectByUserId")
    @ResponseBody
    public ApiResult selectByUserId(Long id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",id);
        List<Good> goods = goodService.list(queryWrapper);
        for (int i = 0; i < goods.size(); i++) {
            Good good = goods.get(i);
            //查询商品分类
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("id",good.getCommodityId());
            CommodityClassification commodityClassification = commodityClassificationService.getOne(queryWrapper1);
            goods.get(i).setCommodityName(commodityClassification.getName());
            //查询发呢类名臣
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("id",good.getClassificationId());
            Classification classification = classificationService.getOne(queryWrapper2);
            goods.get(i).setClassificationName(classification.getName());
        }
        return ApiResult.ok(goods);
    }

    /**
     * @param id
     * @return 根据id查询商品分类
     */
    @RequestMapping(value = "selectCommodityId")
    @ResponseBody
    public CommodityClassification selectCommodityId(Long id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        return commodityClassificationService.getOne(queryWrapper);
    }



    /**
     * @param id
     * @return 根据id查询类型
     */
    @RequestMapping(value = "selectByState")
    @ResponseBody
    public ApiResult selectByState(Long id){
        return ApiResult.ok(moneyService.getById(id));
    }


    /**
     * @param commodityId
     * @param classificationId
     * @return 根据商品分类和分类名称查询规格大小
     */
    @RequestMapping(value = "selectSize")
    @ResponseBody
    public ApiResult selectSize(Long commodityId,Long classificationId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("commodity_id",commodityId);
        queryWrapper.eq("classification_id",classificationId);
        List<SizeClassification> sizeClassifications = sizeClassificationService.list(queryWrapper);
        return ApiResult.ok(sizeClassifications);
    }

}
